﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Task.Model;
using Yipusoft.Account.Model;

public partial class Task_TaskConvey : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    void Bind()
    {
        BindDepartmentID();
    }


    void BindDepartmentID()
    {
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        List<TaskUserInfo> list= Yipusoft.Task.BLL.TaskUser.GetTaskUsers(int.Parse(Request["ID"]));
        List<DepartmentInfo> dptList = Yipusoft.Account.BLL.Department.GetList("Left(DepartmentID,4)='" + user.DepartmentID.Substring(0, 4) + "' And (SubString(DepartmentID,5,1)='2' OR SubString(DepartmentID,5,1)='3')");
        var result=from o in dptList where !(from obj in list select obj.UserId).Contains(o.DepartmentID) select o;
        this.DepartmentList.DataSource = result;
        this.DepartmentList.DataValueField = "DepartmentID";
        this.DepartmentList.DataTextField = "FullName";
        this.DepartmentList.DataBind();
    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {

        List<Departments> Selectlist = null;
        if (ViewState["List"] != null)
            Selectlist = ViewState["List"] as List<Departments>;
        else
            Selectlist = new List<Departments>();

        foreach (ListItem item in DepartmentList.Items)
        {
            if (item.Selected)
            {
                if (Selectlist.Where(o => o.DepartmentID == item.Value).FirstOrDefault() == null)
                    Selectlist.Add(new Departments() { DepartmentID = item.Value, DepartmentName = item.Text });
            }
        }

        ViewState["List"] = Selectlist;
        SetItems();

    }
    protected void ButtonAddAll_Click(object sender, EventArgs e)
    {
        List<Departments> Selectlist = null;
        if (ViewState["List"] != null)
            Selectlist = ViewState["List"] as List<Departments>;
        else
            Selectlist = new List<Departments>();

        foreach (ListItem item in DepartmentList.Items)
        {
            if (Selectlist.Where(o => o.DepartmentID == item.Value).FirstOrDefault() == null)
                Selectlist.Add(new Departments() { DepartmentID = item.Value, DepartmentName = item.Text });

        }

        ViewState["List"] = Selectlist;
        SetItems();
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        List<Departments> Selectlist = null;
        if (ViewState["List"] != null)
            Selectlist = ViewState["List"] as List<Departments>;
        foreach (ListItem item in SelectDepartmentList.Items)
        {
            if (item.Selected)
            {
                Selectlist.Remove(Selectlist.Where(o => o.DepartmentID == item.Value).FirstOrDefault());
            }
        }
        ViewState["List"] = Selectlist;
        SetItems();

    }
    protected void ButtonRemoveAll_Click(object sender, EventArgs e)
    {

        ViewState["List"] = null;
        SelectDepartmentList.Items.Clear();
    }

    void SetItems()
    {
        SelectDepartmentList.Items.Clear();

        List<Departments> Selectlist = null;
        if (ViewState["List"] != null)
            Selectlist = ViewState["List"] as List<Departments>;
        else
            return;
        foreach (Departments dpt in Selectlist)
        {
            SelectDepartmentList.Items.Add(new ListItem() { Value = dpt.DepartmentID, Text = dpt.DepartmentName });
        }


    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        lblPrompt.Text = "";
        List<Departments> Selectlist = null;
        if (ViewState["List"] != null)
            Selectlist = ViewState["List"] as List<Departments>;
        else
            return;
        Yipusoft.Task.Model.TaskUserInfo userInfo = Yipusoft.Task.BLL.TaskUser.GetTaskUser(int.Parse(Request["ID"]), Request["UserID"]);
        foreach (Departments dpt in Selectlist)
        {
            userInfo.UserId = dpt.DepartmentID;
            userInfo.Score = 0;
            userInfo.Progress = 0;
            Yipusoft.Task.BLL.TaskUser.Insert(userInfo);
        }
        lblPrompt.Text = "任务已转达";
    }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserTasks.aspx");
    }
}

[Serializable]
public class Departments
{
    public string DepartmentID
    {
        set;
        get;
    }
    public string DepartmentName
    {
        set;
        get;
    }
}
